﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Print_UI
{
    public partial class FrmSubjectSetting : CommonForm
    {
        #region Contructors
        public FrmSubjectSetting()
        {
            InitializeComponent();
        }
        #endregion
        #region Private members
        private int _id = -1;
        DataTable table = new DataTable();
        
        #endregion
        #region Event
        //load form SubjectSeeting
        private void FrmSubjectSetting_Load(object sender, EventArgs e)
        {
            LoadYear();
         // LoadSemester();
           // LoadClass();  
        }
        #region Loaddata
        private void cboClass_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
            checkused();
        }
        private void cboYear_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
            LoadSemester();
            LoadClass();
            checkused();
        }
        private void cboSemester_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
            LoadClass();
            checkused();
        }
        #endregion
        //button Cancel
        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
           
        }
        //button save
        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //check semester is used
            int year = Convert.ToInt32(cboYear.EditValue);
            int semester = Convert.ToInt32(cboSemester.EditValue);
            bool check = Model.PRI_SemesterSetting.Where(c => c.YearID == year && c.SemesterID == semester && c.IsUsed == true).Count() > 0;
            if (check)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    int id = Convert.ToInt32(table.Rows[i]["ID"]);
                    bool ismain = Convert.ToBoolean(table.Rows[i]["Môn Học Chính"]);
                    var obj = Model.PRI_SubjectSetting.FirstOrDefault(c => c.ID == id);
                    obj.ModifiedDate = DateTime.Now;
                    obj.ModifiedBy = UserName;
                    obj.IsMain = ismain;
                    obj.DepartmentID = DepartmentID;
                    Model.SaveChanges();
                }

                CommonMessage("Cập nhật thành công");
                _id = -1;
            }
            else
            {
                btnSave.Enabled=false;
            }
        }
        #endregion
        #region Private members
        //check year is used
        private void checkused()
        { int year = Convert.ToInt32(cboYear.EditValue);
            int semester = Convert.ToInt32(cboSemester.EditValue);
            bool check = Model.PRI_SemesterSetting.Where(c => c.YearID == year && c.SemesterID == semester && c.IsUsed == true).Count() > 0;
            if (check)
            {
                btnSave.Enabled = true;
            }
            else
            {
                btnSave.Enabled=false;
            }
        }

        //load combobox year
        private void LoadYear()
        {
            var Year = (from p in Model.CAT_Year
                        join k in Model.PRI_SemesterSetting on p.ID equals k.YearID    
                        select new { p.ID, p.Name }).Distinct();
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(-1, "--- Chọn Năm Nọc ---");
            foreach (var item in Year)
            {
                dic.Add(item.ID, item.Name);
            }

            cboYear.Properties.ValueMember = "Key";
            cboYear.Properties.DisplayMember = "Value";
            cboYear.Properties.DataSource = dic;
            cboYear.EditValue =_id;
        }
        //load combobox semester
        private void LoadSemester()
        {
            int i = Convert.ToInt32(cboYear.EditValue);
            var Semester = (from p in Model.CAT_Semester
                            join k in Model.PRI_SemesterSetting on p.ID equals k.SemesterID
                            where i == k.YearID
                            select new { p.ID, p.SemesterName }).Distinct();
            Dictionary<int, string> dic = new Dictionary<int, string>();
            dic.Add(-1, "--- Chọn Học Kì ---");
            foreach (var item in Semester)
            {
                dic.Add(item.ID, item.SemesterName);
            }
            cboSemester.Properties.ValueMember = "Key";
            cboSemester.Properties.DisplayMember = "Value";
            cboSemester.Properties.DataSource = dic;
            cboSemester.EditValue = _id;
        }
        //load combobox class
        private void LoadClass()
        {

            try
            {
                int iYear = Convert.ToInt32(cboYear.EditValue);
                int iSemester = Convert.ToInt32(cboSemester.EditValue);
                var GetClass = (from obj in Model.PRI_SubjectSetting.Where(c => c.DepartmentID == DepartmentID && c.YearID == iYear && c.SemesterID == iSemester)
                                select new { obj.CAT_Class.ID, obj.CAT_Class.Name }).Distinct();
                Dictionary<int, string> dic = new Dictionary<int, string>();
                dic.Add(-1, "--- Chọn Lớp ---");
                foreach (var item in GetClass)
                {
                    dic.Add(item.ID, item.Name);
                }
                cboClass.Properties.ValueMember = "Key";
                cboClass.Properties.DisplayMember = "Value";
                cboClass.Properties.DataSource = dic;
                cboClass.EditValue = -1;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        //load data in datagridview
        private void LoadData()
        {
            
            table.Columns.Clear();
            table.Clear();
            int year = Convert.ToInt32(cboYear.EditValue);
            int semester = Convert.ToInt32(cboSemester.EditValue);
            int Class = Convert.ToInt32(cboClass.EditValue);
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Mã Môn Học", typeof(string));
            table.Columns.Add("Tên Môn Học", typeof(string));
            table.Columns.Add("Môn Học Chính", typeof(bool)); 
            var query = from d in Model.PRI_Subjects
                        join c in Model.PRI_SubjectSetting on d.ID equals c.SubjectID
                        where c.YearID == year
                        && c.SemesterID == semester
                        && c.ClassID == Class
                        && c.DepartmentID == DepartmentID
                        select new
                        {
                            c.ID,
                            d.Code,
                            d.SubjectName,
                            c.IsMain
                        };
            query.ToList().ForEach((n) =>
            {
                DataRow row = table.NewRow();
                row["ID"] = n.ID;
                row["Mã Môn Học"] = n.Code;
                row["Tên Môn Học"] = n.SubjectName;
                row["Môn Học Chính"] = n.IsMain;
                table.Rows.Add(row);
            });
            DataView view = new DataView(table);
            DataTable ew = view.ToTable(true, "Mã Môn Học", "Tên Môn Học", "Môn Học Chính");
            gridControl1.DataSource = ew;
            //gridView1.Columns["ID"].Visible = false;
        }
        #endregion

    }
}
